package com.lv.service.impl;

import com.lv.mapper.TbGoodsDescMapper;
import com.lv.mapper.TbGoodsMapper;
import com.lv.mapper.TbItemCatMapper;
import com.lv.mapper.TbItemMapper;
import com.lv.pojo.TbGoods;
import com.lv.pojo.TbGoodsDesc;
import com.lv.pojo.TbItem;
import com.lv.pojo.TbItemCat;
import com.lv.service.GoodsService;
import com.lv.vo.GoodsVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper gm;

    @Resource
    private TbGoodsDescMapper dm;

    @Resource
    private TbItemCatMapper cm;

    @Resource
    private TbItemMapper im;

    @Transactional
    @Override
    public Integer addGoodsAndDese(TbGoods goods, TbGoodsDesc dase, List<TbItem> parse , List<TbItem> name) {
        goods.setAuditStatus("0");
        goods.setIsMarketable("1");
        Integer i = gm.insertSelective(goods);
        dase.setGoodsId(goods.getId());
        Integer d = dm.insertSelective(dase);
        String goodsId = String.valueOf(goods.getId());
        parse.forEach(item -> {
            item.setSellerId(goodsId);
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setCategoryid(goods.getBrandId());
            item.setStatus("1");
            name.forEach(t -> {
                item.setTitle(t.getTitle());
            });
            Integer ia = im.insertSelective(item);
        });
        return 1;
    }
    //商品审核
    @Override
    public List<GoodsVO> getUp() {
        //查出全部
        List<TbGoods> tbGoods = gm.selectByExample(null);
        List<GoodsVO> list = new ArrayList();

        for (TbGoods tbGood : tbGoods) {
            GoodsVO vo = new GoodsVO();
            //商品ID
            vo.setId(tbGood.getId());
            //商品名称
            vo.setGoodsName(tbGood.getGoodsName());
            //商品价格
            vo.setPrice(tbGood.getPrice());
            //商品状态
            if (tbGood.getAuditStatus().equals("0")){
                vo.setAuditStatus("未审核");
            }
            if (tbGood.getAuditStatus().equals("1")){
                vo.setAuditStatus("审核通过");
            }
            if (tbGood.getAuditStatus().equals("2")){
                vo.setAuditStatus("审核未通过");
            }
            //一级分类
            if (tbGood.getCategory1Id() == null) {
                vo.setName1(null);
            }else {
                TbItemCat c1 = cm.selectByPrimaryKey(tbGood.getCategory1Id());
                vo.setName1(c1.getName());
            }
            //二级分类
            if (tbGood.getCategory2Id() == null) {
                vo.setName2(null);
            }else {
                TbItemCat c2 = cm.selectByPrimaryKey(tbGood.getCategory2Id());
                vo.setName2(c2.getName());
            }
            //三级分类
            if (tbGood.getCategory3Id() == null) {
                vo.setName3(null);
            }else {
                TbItemCat c3 = cm.selectByPrimaryKey(tbGood.getCategory3Id());
                vo.setName3(c3.getName());
            }
            list.add(vo);
        }
        return list;
    }
    //审核

    @Override
    public Integer up(String auditStatus, String[] ids) {
        for (String id : ids) {
            gm.updateUp(id,auditStatus);
        }
        return 1;
    }

    @Override
    public Integer del(Long[] ids) {
        for (Long id : ids) {
            gm.deleteByPrimaryKey(id);
        }
        return 1;
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return gm.selectByPrimaryKey(goodsId);
    }
}
